package com.thoughtservice.jspdoc.export.argouml;

/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is "JSP Documentation Generator Project".
 *
 */

import com.thoughtservice.jspdoc.*;

/**
 * Creates PGML documents of ArgoUML Project files
 *
 * @author Jochen Luell (jochen@luell.com)
 */
public class PgmlDocument {
  private StringBuffer pgmlOut;
    
    // Define string constans for PGML file
  private final static String PGML_HEAD = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n" +
                                  "<!DOCTYPE pgml SYSTEM \"pgml.dtd\"> \r\n" +
                                  "<pgml description=\"org.argouml.uml.diagram.static_structure.ui.UMLClassDiagram|10-48--7-111-ad483:f0a47428ae:-8000\" " +
                                  "name=\"PGML-NAME\"> \r\n";

  private final static String PGML_FOOTER = "</pgml>\r\n";

  private final static String PGML_DEPENDENCY = "<group name=\"GROUP-NAME\"\r\n" +
                "       description=\"org.argouml.uml.diagram.ui.DEP-TYPE\"\r\n" +
                "       href=\"GROUP-HASH\"\r\n" +
                "       stroke=\"1\"\r\n" +
                "       strokecolor=\"-16777216\"\r\n" +
                "  >\r\n" +
                "    <private>\r\n" +
                "      sourcePortFig=\"SOURCE-NAME.0\"\r\n" +
                "      destPortFig=\"DEST-NAME.0\"\r\n" +
                "      sourceFigNode=\"SOURCE-NAME\"\r\n" +
                "      destFigNode=\"DEST-NAME\"\r\n" +
                "    </private>\r\n" +
                "    <path name=\"GROUP-NAME.1\"\r\n" +
                "      description=\"org.tigris.gef.presentation.FigPoly\"\r\n" +
                "      fill=\"0\"\r\n" +
                "      fillcolor=\"-1\"\r\n" +
                "      stroke=\"1\"\r\n" +
                "      strokecolor=\"-16777216\"\r\n" +
                "    >\r\n" +
                "      <moveto x=\"SOURCE-X\"\r\n" +
                "              y=\"SOURCE-Y\" />\r\n" +
                "      <lineto x=\"DEST-X\"\r\n" +
                "              y=\"DEST-Y\" />\r\n" +
                "    </path>\r\n" +
                "  </group>\r\n";

  private final static String PGML_CONTENT = "  <group name=\"GROUP-NAME\"\r\n" +
                "       description=\"org.argouml.uml.diagram.static_structure.ui.FigClass[GROUP-X, GROUP-Y, GROUP-WIDTH, GROUP-HEIGHT]\"\r\n" +
                "       href=\"GROUP-HASHCODE\"\r\n" +
                "       fill=\"1\"\r\n" +
                "       fillcolor=\"-1\"\r\n" +
                "       stroke=\"1\"\r\n" +
                "       strokecolor=\"-16777216\"\r\n" +
                "  >\r\n" +
                "    <private>\r\n" +
                "    </private>\r\n" +
                "    \r\n" +
                "    <rectangle name=\"GROUP-NAME.0\"\r\n" +
                "      x=\"GROUP-X\"\r\n" +
                "      y=\"GROUP-Y\"\r\n" +
                "      width=\"GROUP-WIDTH\"\r\n" +
                "      height=\"GROUP-HEIGHT\"\r\n" +
                "      fill=\"1\"\r\n" +
                "      fillcolor=\"-1\"\r\n" +
                "      stroke=\"1\"\r\n" +
                "      strokecolor=\"-16777216\"\r\n" +
                "    />\r\n" +
                "    <text name=\"GROUP-NAME.1\"\r\n" +
                "      x=\"GROUP-X\"\r\n" +
                "      y=\"GROUP-Y\"\r\n" +
                "      fill=\"1\"\r\n" +
                "      fillcolor=\"-1\"\r\n" +
                "      stroke=\"1\"\r\n" +
                "      strokecolor=\"-16777216\"\r\n" +
                "      font=\"dialog\"\r\n" +
                "      textsize=\"9\"\r\n" +
                "    ></text>\r\n" +
                "    <text name=\"GROUP-NAME.2\"\r\n" +
                "      x=\"GROUP-X\"\r\n" +
                "      y=\"GROUP-Y\"\r\n" +
                "      fill=\"1\"\r\n" +
                "      fillcolor=\"-1\"\r\n" +
                "      stroke=\"1\"\r\n" +
                "      strokecolor=\"-16777216\"\r\n" +
                "      font=\"dialog\"\r\n" +
                "      textsize=\"9\"\r\n" +
                "    >JSP-NAME</text>\r\n" +
                "    <rectangle name=\"GROUP-NAME.3\"\r\n" +
                "      x=\"GROUP-X\"\r\n" +
                "      y=\"GROUP-Y-THIRD\"\r\n" +
                "      width=\"GROUP-WIDTH\"\r\n" +
                "      height=\"2\"\r\n" +
                "      fill=\"1\"\r\n" +
                "      fillcolor=\"-1\"\r\n" +
                "      stroke=\"1\"\r\n" +
                "      strokecolor=\"-1\"\r\n" +
                "    />\r\n" +
                "    <group name=\"GROUP-NAME.4\"\r\n" +
                "         description=\"org.tigris.gef.presentation.FigGroup[GROUP-X, GROUP-Y-THIRD, GROUP-WIDTH, GROUP-HEIGHT-THIRD]\"\r\n" +
                "         fill=\"1\"\r\n" +
                "         fillcolor=\"-1\"\r\n" +
                "         stroke=\"1\"\r\n" +
                "         strokecolor=\"-16777216\"\r\n" +
                "    >\r\n" +
                "      <private>\r\n" +
                "      </private>\r\n" +
                "      \r\n" +
                "      <rectangle name=\"GROUP-NAME.4.0\"\r\n" +
                "        x=\"GROUP-X\"\r\n" +
                "        y=\"GROUP-Y-THIRD-2\"\r\n" +
                "        width=\"GROUP-WIDTH\"\r\n" +
                "        height=\"GROUP-HEIGHT-THIRD\"\r\n" +
                "        fill=\"1\"\r\n" +
                "        fillcolor=\"-1\"\r\n" +
                "        stroke=\"1\"\r\n" +
                "        strokecolor=\"-16777216\"\r\n" +
                "      />\r\n" +
                "    </group>\r\n" +
                "    <group name=\"GROUP-NAME.5\"\r\n" +
                "         description=\"org.tigris.gef.presentation.FigGroup[GROUP-X, GROUP-Y-THIRD-2, GROUP-WIDTH, GROUP-HEIGHT-THIRD]\"\r\n" +
                "         fill=\"1\"\r\n" +
                "         fillcolor=\"-1\"\r\n" +
                "         stroke=\"1\"\r\n" +
                "         strokecolor=\"-16777216\"\r\n" +
                "    >\r\n" +
                "      <private>\r\n" +
                "      </private>\r\n" +
                "      \r\n" +
                "      <rectangle name=\"GROUP-NAME.5.0\"\r\n" +
                "        x=\"GROUP-X\"\r\n" +
                "        y=\"GROUP-Y-THIRD-2\"\r\n" +
                "        width=\"GROUP-WIDTH\"\r\n" +
                "        height=\"GROUP-HEIGHT-THIRD\"\r\n" +
                "        fill=\"1\"\r\n" +
                "        fillcolor=\"-1\"\r\n" +
                "        stroke=\"1\"\r\n" +
                "        strokecolor=\"-16777216\"\r\n" +
                "      />\r\n" +
                "    </group>\r\n" +
                "  </group>\r\n";

  public final static String PGML_USECASE_DOCUMENT = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n" +
                "<!DOCTYPE pgml SYSTEM \"pgml.dtd\">\r\n" +
                "<pgml description=\"org.argouml.uml.diagram.use_case.ui.UMLUseCaseDiagram|10-48--7-111-ad483:f0a47428ae:-8000\"\r\n" +
                "      name=\"use case diagram 1\"\r\n" +
                ">\r\n" +
                "</pgml>\r\n";


  /**
   * Class constructor
   */
  public PgmlDocument() {
         pgmlOut = new StringBuffer();
  }

  /**
   * Creates the PGML header
   */
  public void createHeader(String name) {
          try {
	    String out = PGML_HEAD;
	    RE re;

	    re = new RE("PGML-NAME");
	    out = re.subst(out, name);
	    
	    pgmlOut.append(out);
          }
	  catch(Exception ex) {
		  ex.printStackTrace();
          }
  }

  /**
   * Creates the PGML footer
   */
  public void createFooter() {
          pgmlOut.append(PGML_FOOTER);
  }

  /**
   * Adds an PGML Group (Class representation)
   *
   * @param e ca.yourdecor.jspdoc.export.argouml.GroupElement Element definition
   * @see com.thoughtservice.jspdoc.export.argouml.GroupElement
   */
  public void addGroup(GroupElement e) {
	  try {
	    String out = PGML_CONTENT;
	    RE re;

	    re = new RE("GROUP-X");
	    out = re.subst(out, "" + e.getXcoord());

	    re = new RE("GROUP-Y-THIRD-2");
	    out = re.subst(out, "" + (e.getYcoord() + (e.getHeight() / 3) * 2));

            re = new RE("GROUP-Y-THIRD");
	    out = re.subst(out, "" + (e.getYcoord() + (e.getHeight() / 3)));

	    re = new RE("GROUP-Y");
	    out = re.subst(out, "" + e.getYcoord());

	    re = new RE("GROUP-WIDTH");
	    out = re.subst(out, "" + e.getWidth());


	    re = new RE("GROUP-HEIGHT-THIRD");
	    out = re.subst(out, "" + (e.getHeight() / 3));

	    re = new RE("GROUP-HEIGHT");
	    out = re.subst(out, "" + e.getHeight());

	    re = new RE("GROUP-NAME");
	    out = re.subst(out, e.getGroupName());

	    re = new RE("JSP-NAME");
	    out = re.subst(out, e.getJspName());

	    re = new RE("GROUP-HASHCODE");
	    out = re.subst(out, "" + e.getJspName().hashCode() + "-" + e.getLinkTypeTo());

            pgmlOut.append(out);
	  }
	  catch(Exception ex) {
		  ex.printStackTrace();
          }
  }

  /**
   * Adds an PGML Group dependencies
   *
   * @param source ca.yourdecor.jspdoc.export.argouml.GroupElement Source element definition
   * @param dest ca.yourdecor.jspdoc.export.argouml.GroupElement Destination element definition
   * @param groupNum int Group number for dependency to insert
   * @see com.thoughtservice.jspdoc.export.argouml.GroupElement
   */
  public void addDependency(GroupElement source, GroupElement dest, int groupNum) {
	  try {
	    String out = PGML_DEPENDENCY;

	    int sourceX = source.getXcoord();
	    int destX = dest.getXcoord();

	    if(source.getLevel() < dest.getLevel()) {
	        sourceX += source.getWidth();
	    }
	    else if(source.getLevel() > dest.getLevel()) {
	        destX += dest.getWidth();
	    }

	    RE re;

	    re = new RE("GROUP-HASH");
	    out = re.subst(out, source.getJspName().hashCode() + "-" +
	                  dest.getJspName().hashCode() + "-" + source.getLinkTypeTo());

	    re = new RE("GROUP-NAME");
	    out = re.subst(out, "Fig" + groupNum);

            re = new RE("SOURCE-NAME");
	    out = re.subst(out, source.getGroupName());

	    re = new RE("DEST-NAME");
	    out = re.subst(out, dest.getGroupName());

            re = new RE("SOURCE-X");
	    out = re.subst(out, "" + sourceX);

	    re = new RE("SOURCE-Y");
	    out = re.subst(out, "" + (source.getYcoord() + source.getHeight() / 2));

            re = new RE("DEST-X");
	    out = re.subst(out, "" + destX);

	    re = new RE("DEST-Y");
	    out = re.subst(out, "" + (dest.getYcoord() + dest.getHeight() / 2));

            re = new RE("DEP-TYPE");
	    //out = re.subst(out, "FigGeneralization");
	    out = re.subst(out, "FigDependency");

            pgmlOut.append(out);
	  }
	  catch(Exception e) {
		  e.printStackTrace();
          }
  }

  /**
   * Returns the PGML document
   *
   * @return java.lang.String The PGML document
   */
  public String getDocument() {
      return pgmlOut.toString();
  }

}
